package controller;

import javax.swing.SwingWorker;

import model.FilesFinder;
import model.Movie;
import model.MoviesList;
import model.Storage;
import view.ImportedMoviesView;
import view.MainView;

/**.
 * Main Controller
 * @author jaubertbenjamin
 *
 */
public class MainController {

    /**.
     * movie list
     */
    private static MoviesList list;
    /**.
     * storage
     */
    private static Storage storage;
    /**.
     * main view
     */
    private static MainView mainView;
    /**.
     * pop up view used to said that the movies are imported
     */
    private static ImportedMoviesView popUp;
    /**.
     * path used to know where is the saving file
     */
    private static String path;
    /**.
     *  check if the saving file is in json or not
     */
    private static boolean isJson;

     /**.
     * Main function
     * @param args
     */
     public static void main(final String[] args) {
        storage = new Storage();
        if (listExist()) {
            mainView = new MainView(list.transformIntoArray() , list.findMoviesNotCompleted(), list.getMovies().get(0));
        } else {
            mainView = new MainView(null , null , null);
        }
     }

     /**.
     * Launch a specific movie found by its title
     * @param title
     */
    public static void launchMovie(final String title) {
        Movie movie = getMovie(title);
        if (movie != null) {
            movie.play();
        }
    }

    /**.
    * Launch a movie by its path
    */
    public static void launchMovieByPath(final String path) {
        Movie movie = list.findMovieByPath(path);
        if (movie != null) {
            movie.play();
        }
    }

     /**.
     * Found a movie by its title
     * @param title
     * @return movie
     */
    public static Movie getMovie(final String title){
       return list.findMovie(title);
    }

     /**.
     * Organize the movie folder
     * @param path
     */
    public static void orderMovies(final String path){
        if (list != null) {
            list.order(path);
            if (isJson) {
                saveJSON(MainController.path);
            } else {
                saveXML(MainController.path);
            }
        }
    }

     /***.
     * Add new movies to library
     * @param path
     */
     public static void addNewMovies(final String path){
         if (path != null) {
             SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
                @Override
                protected Void doInBackground() throws Exception {
                    list = FilesFinder.findMovies(list, path);
                    if (MainController.path != null) {
                        if (isJson) {
                            saveJSON(MainController.path);
                        } else {
                            saveXML(MainController.path);
                        }
                    }
                    mainView.updateList(list.transformIntoArray());
                    mainView.SetMovieInfo(list.returnFirstAvailableMovie());
                    mainView.updateListMoviesNotFound(list.findMoviesNotCompleted());
                    return null;
                }
                protected void done() { 
                    popUp.dispose();
                    if (MainController.path == null) {
                        MainController.path = mainView.setSaveChooser(".json");
                        saveJSON(MainController.path);
                    }
                }
            };
            worker.execute();
            popUp = new ImportedMoviesView();		
		}
	}

	/**.
	 * Save library in xml
	 */
    public static void saveXML(String path) {
		if (path != null) {
			storage.xmlSerialization(list, path);
		}
	}

	/**.
	 * Load a xml file
	 */
	public static void loadXML(final String path){
		MainController.path = path;
		isJson = false;
		list = storage.xmlDeserialization(path);
		mainView.updateList(list.transformIntoArray());
		mainView.SetMovieInfo(list.returnFirstAvailableMovie());
		mainView.updateListMoviesNotFound(list.findMoviesNotCompleted());

	}

	/**.
	 * check if the movies list exist into the application
	 * @return true if exists else false
	 */
	public static boolean listExist() {
		return list != null;
	}

	/**.
	 * save library in json
	 */
	public static void saveJSON(final String path) {
		if (path != null) {
			storage.jsonSerialization(list, path);
		}
	}

	/**.
	 * load a json file
	 */
	public static void loadJSON(final String path) {
		MainController.path = path;
		isJson = true;
		list = storage.jsonDeserialization(path);
		mainView.updateList(list.transformIntoArray());
		mainView.SetMovieInfo(list.returnFirstAvailableMovie());
		mainView.updateListMoviesNotFound(list.findMoviesNotCompleted());
	}

	/**.
	 * Find movies in the movies list
	 * @param search
	 */
	public static void findMovies(final String search) {
		if (list != null) {
			mainView.updateList(list.searchMovies(search));
		}
	}

	/**.
	 * retrieve the full library
	 */
	public static void getFullLibrary() {
		if (list != null) {
			mainView.updateList(list.transformIntoArray());
		}
	}

}
